const express = require('express');
const bodyParser = require('body-parser');
const cookieSession = require('cookie-session');
const path = require('path');

const login = require('./modules/user/login');
const logout = require('./modules/user/logout');
const isLogin = require('./modules/user/isLogin');
const changePassword = require('./modules/user/changePassword');

const addClerk = require('./modules/clerk/addClerk');
const viewClerk = require('./modules/clerk/viewClerk');
const editClerk = require('./modules/clerk/editClerk');


const addMedicine = require('./modules/medicine/addMedicine');
const viewMedicine = require('./modules/medicine/viewMedicine');
const editMedicine = require('./modules/medicine/editMedicine');

const addBill = require('./modules/bill/addBill');
const viewBill = require('./modules/bill/viewBill');


const uploader = require('./modules/tools/upload');
const multer = require('multer');//接收图片
const upload = multer({
    dest: '/public/assets'
});//定义图片上传的临时目录

const app = express();

//静态资源
app.use('/assets', express.static('assets'));
app.use(express.static('public'));


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

app.use(cookieSession({
  name: 'session',
  keys: ['medicine','system'],
  maxAge: 24 * 60 * 60 * 1000 
}))


app.get('/api/isLogin', isLogin);
app.post('/api/login', login);
app.post('/api/logout', logout);
app.post('/api/changePassword', changePassword);

app.post('/api/addClerk', addClerk);
app.post('/api/viewClerk', viewClerk);
app.post('/api/editClerk', editClerk);

app.post('/api/addMedicine', addMedicine);
app.post('/api/viewMedicine', viewMedicine);
app.post('/api/editMedicine', editMedicine);

app.post('/api/addBill', addBill);
app.post('/api/viewBill', viewBill);

// 单域单文件上传
app.post('/api/upload', upload.single('imgfile'), uploader)



const server = app.listen(3000, ()=>{
  let host = server.address().address;
  let port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});